<?php

class Table {

	var $oDatabase;

	var $sName;
	var $aColumns;

	var $oPrimaryKey;
	var $aForeignKeysReferencingThisTable;
	var $aForeignKeysFromThisTable;
	
	function Table ( $sName ) {
		$this->sName            = $sName;
		$this->aColumns         = array();
		$this->aForeignKeysReferencingThisTable = array();
		$this->aForeignKeysFromThisTable        = array();
	}

	function SetDatabase( &$oDatabase ) {
		$this->oDatabase =& $oDatabase;
	}

	function HasForeignKeysFrom() {
		return !empty( $this->aForeignKeysFromThisTable );
	}

	function HasForeignKeysReferencing() {
		return !empty( $this->aForeignKeysReferencingTable );
	}

	function AddColumn ( $sName, $sDataType ) {
		$this->aColumns[ $sName ] = new Column( $this, $sName, $sDataType );	
	}

	function AddPrimaryKey( $sName, $aColumnNames ) {
		$this->oPrimaryKey = new PrimaryKey( $sName, $this );
		foreach ( $aColumnNames as $sThisColumnName ) {
			$oColumn =& $this->GetColumn( $sThisColumnName );
			$this->oPrimaryKey->AddColumn( $oColumn );	
		}
	}

	function AddForeignKeyReferencingThisTable( &$oForeignKey ) {
		$this->aForeignKeysReferencingThisTable[ $oForeignKey->GetName() ] =& $oForeignKey;
	}
	
	function & AddForeignKeyFromThisTable( &$oForeignKey, $aFromColumnListAsStrings ) {
		$aFromColumnList = array();
		foreach ( $aFromColumnListAsStrings as $sColumnName ) {

			$oColumn =& $this->GetColumn( $sColumnName );
			if ( is_object( $oColumn ) ) {
				$oColumn->SetIsPartOfForeignKey();
				$aFromColumnList[] =& $oColumn;
			} else {
				echo( 'Couldn\'t find column ' . $this->GetName() . '.' . $sColumnName . ' for the foreign key ' . $oForeignKey->GetName() . "\n" );
			}
		}

		$this->aForeignKeysFromThisTable[ $oForeignKey->GetName() ] =& $oForeignKey;
		return $aFromColumnList;
	}

	function & GetColumn( $sName ) {
		$oColumn = null;
		if ( isset( $this->aColumns[ $sName ] ) ) {
			$oColumn =& $this->aColumns[ $sName ];	
		}
		return $oColumn;
	}

	function GetName() {
		return $this->sName;	
	}
	
	function & GetColumns() {
		return $this->aColumns;
	}

	function GetColumnNames() {
		return array_keys( $this->aColumns );
	}

	function GetColumnDataTypes() {
		$aColumnDataTypes = array();
		foreach ( $this->aColumns as $oThisColumn ) {
			$aColumnDataTypes[ $oThisColumn->GetName() ] = $oThisColumn->GetDataType();
		}
		return $aColumnDataTypes;
	}

	function GetColumnProperties() {
		$aColumnProperties = array();
		foreach ( $this->aColumns as $oThisColumn ) {
			$aColumnProperties[ $oThisColumn->GetName() ] = $oThisColumn->GetPropertiesAsString();
		}
		return $aColumnProperties;
	}

	function & GetPrimaryKey() {
		return $this->oPrimaryKey;	
	}
	
	function & GetForeignKeysReferencingThisTable() {
		return $this->aForeignKeysReferencingThisTable;	
	}

	function & GetForeignKeysFromThisTable() {
		return $this->aForeignKeysFromThisTable;	
	}

}

require_once( 'Column.php' );
require_once( 'PrimaryKey.php' );

?>
